import { encryptPassword } from '@/utils/utils'

export default {
    type: 'form',
    title: '',
    mode: 'horizontal',
    actions: [],
    wrapWithPanel: false,
    initApi: '/upms/crypto/key',
    canAccessSuperData: false,
    api: {
        url:'/upms/admin/user/add',
        method: 'post',
        requestAdaptor: function (api, context){
          context.password = encryptPassword(context.key, context.password);
          if(context.roleIds && !Array.isArray(context.roleIds)){
            context.roleIds = context.roleIds.split(',');
          }
          return {
            ...api,
            context
          };
        }
    },
    messages:{
        validateFailed:''
    },
    body:[
    {
        type: 'group',
        body:[
            {
                type: 'input-text',
                name: 'username',
                label: '用户名',
                maxLength: 36,
                required: true,
                validationErrors: {
                    isRequired: '请输入用户名'
                }
            },
            {
                type: 'input-text',
                name: 'nickName',
                label: '昵称',
                maxLength: 100,
            },
        ]
    },
    {
        type: 'group',
        body:[
            {
                type: 'select',
                name: 'type',
                label: '用户类型',
                source: '/base/dict/options/user-type',
                required: true,
                validationErrors: {
                  isRequired: '请选择用户类型'
                },
                onEvent: {
                    change: {
                        actions: [
                            {
                                actionType: 'setValue',
                                componentId: 'orgId',
                                args: {
                                    value: null
                                }
                            }
                        ]
                    }
                }
            },
            {
                type: 'input-text',
                name: 'phone',
                label: '绑定手机号',
                maxLength: 20,
            },
        ]
    },
    {
        type: 'group',
        body:[
            {
                type: 'select',
                name: 'orgId',
                id: 'orgId',
                label: '所属机构',
                autoComplete: '/upms/admin/org/select?orgName=${term}&orgType=${type}',
                placeholder: '请选择所属机构'
            },
            {
                type: 'input-text',
                name: 'orgName',
                label: '机构名称',
                maxLength: 100,
                desc: '管理员、专家直接输入机构名称即可'
            },
        ]
    },
    {
        type: 'group',
        body: [
            {
                type: 'select',
                name: 'roleIds',
                label: '选择角色',
                searchable: true,
                multiple: true,
                source: '/upms/admin/role/select',
                clearable: true
            },
            {
                type: 'input-password',
                name: 'password',
                label: '密码',
                required: true,
                value: '',
                maxLength: 30,
                validationErrors: {
                  isRequired: '请输入密码'
                }
            },
        ]
    },
    {
        type: 'group',
        body:[
            {
                type: 'switch',
                name: 'status',
                label: '状态',
                onText: '启用',
                offText: '禁用',
                value: 1,
                trueValue: 1,
                falseValue: 0,
                required: true
            },
            {
                type: 'switch',
                name: 'mainFlag',
                label: '是否主账号',
                value: 1,
                onText: '是',
                offText: '否',
                trueValue: 1,
                falseValue: 0,
                required: true
            },
            {
                type: 'switch',
                name: 'resetPwdFlag',
                label: '是否修改密码',
                value: 0,
                onText: '是',
                offText: '否',
                trueValue: 1,
                falseValue: 0,
                required: true
            },
        ]
    },
    {
        type: 'input-text',
        name: 'remark',
        label: '备注',
        maxLength: 50,
    }
]
}
